package com.springboot.blog.constants;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @Description: 图片上传路径常量
 * @date: 2019/8/24 15:16
 */
@Component
public class UploadConstants {
    public static String BASE_PATH;
    public static String ACCESS_PATH;
    public static Boolean UPLOAD_QINIU;
    public static String HOST;

    @Value("${upload.basePath}")
    public void setUploadBasePath(String basePath) {
        UploadConstants.BASE_PATH = basePath;
    }

    @Value("${upload.accessPath}")
    public void setUploadAccessPath(String accessPath) {
        UploadConstants.ACCESS_PATH = accessPath;
    }

    @Value("${upload.qiniuUpload}")
    public void setUploadQiniu(Boolean uploadQiniu) {
        UploadConstants.UPLOAD_QINIU = uploadQiniu;
    }

    @Value("${upload.host}")
    public void setHost(String host) {
        UploadConstants.HOST = host;
    }

    /**
     * @Description: 获取图片后缀
     * @Param: [file]
     * @return: java.lang.String
     * @date: 2019/8/24 15:27
     */
    public static String getSuffixName(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        return fileName.substring(fileName.lastIndexOf("."));
    }

    /**
     * @Description: 生成文件名称通用方法
     * @Param: [suffixName] 图片后缀
     * @return: java.lang.String
     * @date: 2019/8/24 15:29
     */
    public static String getNewFileName(String suffixName) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
        int random = new Random().nextInt(100);
        StringBuilder tempName = new StringBuilder();
        tempName.append(sdf.format(new Date())).append(random).append(suffixName);
        return tempName.toString();
    }
}
